Skip to main content

Async Load

Piscina supports asynchronously loaded workers. This feature allows you to perform asynchronous operations during worker initialization, such as loading configurations or establishing database connections.

For example, the main script below creates a Piscina pool and runs tasks from two worker files.

main.js
"use strict";

const Piscina = require("../..");

const pool = new Piscina();
const { resolve } = require("path");

(async () => {
await Promise.all([
pool.run({}, { filename: resolve(__dirname, "worker") }),
pool.run({}, { filename: resolve(__dirname, "worker.mjs") }),
]);
})();

Both worker files demonstrate asynchronous loading. They use a sleep function to simulate an asynchronous operation that takes 500 milliseconds.

worker.js
"use strict";

const { promisify } = require("util");
const sleep = promisify(setTimeout);

module.exports = (async () => {
await sleep(500);
return () => console.log("hello from an async loaded CommonJS worker");
})();

In worker.mjs:

worker.mjs
// eslint-disable-next-line no-eval
import util from "util";
const sleep = util.promisify(setTimeout);

async function load() {
await sleep(500);
return () => console.log("hello from an async loaded ESM worker");
}

export default load();

You can also check out this example on github.